//Source file: D:\\test\\com\\ce\\pms\\custservice\\service\\impl\\CodingGeneratorImpl.java

package com.ce.pms.common.service.impl;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.service.ICodingGeneratorService;
import com.ce.pms.custservice.dao.ICustServiceEventDao;
import com.ce.pms.custservice.dao.IDispatchFormDao;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.occupation.dao.IPerfectItemDispatchDao;
import com.ce.pms.renovation.dao.IRenovateLicenceDao;
import com.ce.pms.renovation.dao.IRenovateRequisitionDao;
import com.ce.util.DateUtil;

public class CodingGeneratorServiceImpl extends PmsBaseServiceImpl implements
		ICodingGeneratorService {
	private IDispatchFormDao dispatchFormDao;
	private IRenovateRequisitionDao renovateRequisitionDao;
	private IRenovateLicenceDao renovateLicenceDao;
	private IPerfectItemDispatchDao perfectItemDispatchDao;
	private ICustServiceEventDao custServiceEventDao;

	// 客户事件编号前缀
	private static final String EVENT_SEQUENCE_PREFIXS = "SJ";

	// 派工单编号前缀
	private static final String DISPATCH_SEQUENCE_PREFIXS = "PG";

	//装修申请单号前缀
	private static final String REQUISTION_NUMBER_PREFIXS = "ZX";

	//出入证号前缀
	private static final String PASS_NUMBER_PREFIXS="EN";
	//入伙派工单编号前缀
	private static final String OCCUPATION_SEQUENCE_PREFIXS = "OG";

	//装修申请单号
	private static final String REQUISTION_NUMBER="RequisitionNumber";

	//出入证号
	private static final String PASS_NUMBER="passNumber";

	private Map<String, KeyInfo> keyMap = new HashMap<String, KeyInfo>();

	public String generateCustServEventCodeing() {
		Date now = custServiceEventDao.getDate();
		String key = formatNum(getNextKey("customerEvent", now));
		String dateStr = DateUtil.convertDateToStr(now, "yyyyMMdd");
		StringBuffer sb = new StringBuffer(EVENT_SEQUENCE_PREFIXS).append(
				dateStr).append(key);
		return sb.toString();
	}

	public String generateDispatchFormCoding() {
		Date now = dispatchFormDao.getDate();
		String key = formatNum(getNextKey("dispatchForm", now));
		String dateStr = DateUtil.convertDateToStr(now, "yyyyMMdd");
		StringBuffer sb = new StringBuffer(DISPATCH_SEQUENCE_PREFIXS).append(
				dateStr).append(key);
		return sb.toString();
	}
	public String generateOccupationCoding() {
		Date now = perfectItemDispatchDao.getDate();
		String key = formatNum(getNextKey("perfectItemDispatch", now));
		String dateStr = DateUtil.convertDateToStr(now, "yyyyMMdd");
		StringBuffer sb = new StringBuffer(OCCUPATION_SEQUENCE_PREFIXS).append(
				dateStr).append(key);
		return sb.toString();
	}
	/**
	 * 功能描述:生成装修申请单号<br>
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-4<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String generateRequisitionNumber() {
		Date now = this.getDate();
		String key = formatNum(getNextKey(REQUISTION_NUMBER, now));
		String dateStr = DateUtil.convertDateToStr(now, "yyyyMMdd");
		StringBuffer sb = new StringBuffer(REQUISTION_NUMBER_PREFIXS).append(
				dateStr).append(key);
		return sb.toString();
	}

	/**
	 * 功能描述:生成出入证号<br>
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-4<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String generatePassNumber() {
		Date now = this.getDate();
		String key = formatNum(getNextKey(PASS_NUMBER, now));
		String dateStr = DateUtil.convertDateToStr(now, "yyyyMMdd");
		StringBuffer sb = new StringBuffer(PASS_NUMBER_PREFIXS).append(
				dateStr).append(key);
		return sb.toString();
	}

	/***************************************************************************
	 * 功能描述: 根据key值获取编号<br>
	 *
	 * @param keyName
	 * @param currentDate
	 * @return Create author:yandawei <br>
	 *         Create on:2008-12-18 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private synchronized long getNextKey(String keyName, Date currentDate) {
		KeyInfo keyInfo = null;
		if (keyMap.containsKey(keyName)) {
			keyInfo = keyMap.get(keyName);
			keyInfo.setCurrentDate(currentDate);
		} else {
			keyInfo = new KeyInfo(currentDate, keyName);
			keyMap.put(keyName, keyInfo);
		}
		return keyInfo.getNextKey();
	}

	/***************************************************************************
	 * 功能描述: 前数值前补0转为字符串(如1转为001)<br>
	 *
	 * @param noteCount
	 * @return Create author:yandawei <br>
	 *         Create on:2008-12-18 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private String formatNum(long noteCount) {

		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumIntegerDigits(3);
		numberFormat.setMinimumIntegerDigits(3);
		numberFormat.setGroupingUsed(false);
		return numberFormat.format(noteCount);
	}

	/**
	 * 编号生成类
	 *
	 * @author yandawei
	 *
	 */
	private class KeyInfo {
		private String keyName;

		private long nextKey = 1L;

		private Date saveDate;

		private Date currentDate;

		public KeyInfo(Date currentDate, String keyName) {
			this.saveDate = currentDate;
			this.currentDate = currentDate;
			this.keyName = keyName;
			retrieveFromDB();
		}

		public void setCurrentDate(Date currentDate) {
			this.currentDate = currentDate;
		}

		/**
		 * 功能描述: 获取下一个编号 <br>
		 *
		 * @return Create author:yandawei <br>
		 *         Create on:2008-12-18 <br>
		 *         Edit author: <br>
		 *         Edit on: <br>
		 *         Why: <br>
		 */
		public long getNextKey() {
			if (DateUtil.getDays(saveDate, currentDate) >= 1) {
				nextKey = 1;
				saveDate = currentDate;
			}
			return nextKey++;
		}

		/**
		 * 功能描述: 从数据库获取最大编号<br>
		 *
		 * Create author:yandawei <br>
		 * Create on:2008-12-18 <br>
		 * Edit author: <br>
		 * Edit on: <br>
		 * Why: <br>
		 */
		private void retrieveFromDB() {
			long keyFromDB = 0;
			String keyStr = null;
			if (keyName.equals("dispatchForm")) {
				keyStr = dispatchFormDao.getMaxDispatchFormCodeFromDB();
			} else if (keyName.equals("customerEvent")) {
				keyStr = custServiceEventDao
						.getMaxCustomerServiceEventCodeFromDB();
			} else if (keyName.equals(REQUISTION_NUMBER)){
				keyStr = renovateRequisitionDao.getMaxRequistionNumber();
			} else if (keyName.equals(PASS_NUMBER)){
				keyStr = renovateLicenceDao.getMaxPassNumber();
			}else if (keyName.equals("perfectItemDispatch")){
				keyStr = perfectItemDispatchDao.getMaxDispatchNumber();
			}else {
				throw new BusinessException("not find key");
			}
			if (!BlankUtil.isBlank(keyStr))
				keyFromDB = getMaxKey(keyStr);
			nextKey = ++keyFromDB;
		}

		/**
		 * 功能描述: 将字符编号转为数值 <br>
		 *
		 * @param keyStr
		 * @return Create author:yandawei <br>
		 *         Create on:2008-12-18 <br>
		 *         Edit author: <br>
		 *         Edit on: <br>
		 *         Why: <br>
		 */
		private long getMaxKey(String keyStr) {
			keyStr = keyStr.substring(10);
			Long key = null;
			NumberFormat format = NumberFormat.getInstance();
			format.setMaximumIntegerDigits(3);
			try {
				key = (Long) format.parse(keyStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return key;
		}

	}

	public void setCustServiceEventDao(ICustServiceEventDao custServiceEventDao) {
		this.custServiceEventDao = custServiceEventDao;
	}

	public void setDispatchFormDao(IDispatchFormDao dispatchFormDao) {
		this.dispatchFormDao = dispatchFormDao;
	}

	public void setRenovateRequisitionDao(
			IRenovateRequisitionDao renovateRequisitionDao) {
		this.renovateRequisitionDao = renovateRequisitionDao;
	}

	public void setRenovateLicenceDao(IRenovateLicenceDao renovateLicenceDao) {
		this.renovateLicenceDao = renovateLicenceDao;
	}

	/**
	 * @param perfectItemDispatchDao the perfectItemDispatchDao to set
	 */
	public void setPerfectItemDispatchDao(
			IPerfectItemDispatchDao perfectItemDispatchDao) {
		this.perfectItemDispatchDao = perfectItemDispatchDao;
	}
}
